package com.example.springboot_blog.controller;


import com.example.springboot_blog.domain.Article;
import com.example.springboot_blog.service.Impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/back")
public class BackController {
    @Autowired
    private ArticleServiceImpl articleService;
    /**
     * @Paam:a
     * @Retrrn:a
     * 文章管理页面
     */
    Article article;
    @GetMapping("/article")
    public String articleManager(Model model) {
        List<Article> articles = articleService.selectList(article);
        model.addAttribute("articles", articles);
        return "/back/article_manager";
    }

    /**
     * @Paam:a
     * @Retrrn:a
     * 文章删除
     */
    @GetMapping("/article/delete/{id}")
    public String deleteArticle(@PathVariable("id") Integer id) {
        articleService.delete(id);
        return "redirect:/back/article";
    }

    /**
     * @Param:a
     *@Retrrn:a
     *跳转到文章修改页面
     */
    @GetMapping("/article/edit/{id}")
    public String editArticlePage(@PathVariable("id") Integer id, Model model){
        model.addAttribute("article", articleService.getById(id));
        return "/back/article_edit";
    }


    /**
     * @Param:a
     *@Retrrn:a
     * 文章修改发布
     */
    @PostMapping("/article/publish/{id}")
    public String editArticle(@PathVariable("id") Integer id, @RequestParam("title") String title, @RequestParam("body") String body){
        articleService.updateById(id,title, body);
        return "redirect:/article/"+id;
    }

    /**
     * @Param:a
     *@Retrrn:a
     * 跳转到文章新增页面
     */
    @GetMapping("/article/add")
    public String addArticlePage(){
        return "/back/article_edit";
    }




    /**
     * @Param:a
     *@Retrrn:a
     * 文章新增发布
     */
    @PostMapping("/article/publish")
    public String addArticle(@RequestParam("title") String title, @RequestParam("body") String body){
        Boolean article =  articleService.insert(title, body);
        return "/back/article_success";
    }

}
